<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * CodeIgniter Admin_Class Library
 *
 * @package         CodeIgniter
 * @subpackage      Libraries
 * @category        Libraries
 * @author          xiazhang
 * @license         MIT
 */
class Admin_Class {
    protected $ci;
    private $admin_db;
	private $cookie_domain,$secure,$admin_key;
    public function __construct() {
        $this->ci = & get_instance();
        $this->admin_db = $this->ci->load->database('admin',true);
		$this->secure = $this->ci->config->item('cookie_secure');
		$this->cookie_domain = $this->ci->config->item('cookie_domain');
    }

    /**
     * 加
     * @param array  $data_arr [description]
     * @param int  $dot 保留位数
     * @param string  $write_conn [description]
     */
    public function add($data_arr, $dot=2) {
        foreach ($data_arr as $key => $val) {
            $$key = $val;
        }
        unset($data_arr);
        $rs = false;
        $datetime = date('Y-m-d H:i:s');
        if(!isset($table) || $table=='') $table = 'admin_admin';
        if(!isset($ip) || $ip=='') $ip = '127.0.0.1';
        try {
            $log_arr = array(
				'admin_name'=>$admin_name,
				'admin_status'=>$admin_status,
				'platform'=>$platform,
				'admin_type'=>$admin_type,
				'admin_access'=>$admin_access,
				'admin_login_time'=>$admin_login_time,
				'admin_last_ip'=>$admin_last_ip,
				'admin_password'=>$admin_password,
				'admin_accessgroup'=>$admin_accessgroup,
				'repeat_login'=>$repeat_login,
            );
            if (!$this->admin_db->insert($table, $log_arr)) throw new Exception('L:'.__LINE__." $table add insert error!", 1);
            $rs = true;
        } catch (Exception $e) {
            writeDebug('['.$datetime.'] add admin Err: '.$e->getMessage(), 'Admin');
        }
        return $rs;
    }
	
	//检查是否登录
	public function isLogin($platform,$return=false) {
		$admin_id	=	$this->ci->input->cookie("platform[".$platform."]_admin_login");
		if($return && $admin_id){
			return $this->getAdmin($admin_id);
		}
		return $admin_id;
	}

	//登录
	public function addLoginLog($data_arr) {
        $rs			= false;
        $datetime	= date('Y-m-d H:i:s');
		$ip			= get_ip();
		$table		= 'admin_log';
        try {
			$this->admin_db->set('admin_login_times', 'admin_login_times+1', FALSE);
			$this->admin_db->set('admin_last_time', $datetime, true);
			$this->admin_db->set('admin_last_ip', $ip, true);
            $this->admin_db->where(array('admin_id'=>$data_arr['admin_id']));
            if(!$this->admin_db->update('admin_user')){
                throw new Exception('L:'.__LINE__." admin_user update error!", 1);
            }

            $log_arr = array(
				'admin_id'=>$data_arr['admin_id'],
				'admin_name'=>$data_arr['admin_name'],
				'admin_time'=>$datetime,
				'admin_ip'=> $ip,
				'admin_action'=>$_SERVER['HTTP_USER_AGENT'],
            );
            if (!$this->admin_db->insert($table, $log_arr)) throw new Exception('L:'.__LINE__." $table add insert error!", 1);
            $rs = true;
        } catch (Exception $e) {
            writeDebug('['.$datetime.'] add admin Err: '.$e->getMessage(), 'Admin');
        }
        return $rs;
		return false;
	}

	//操作Log
	public function addOperationLog($log_arr) {
		$table	=	"admin_action";
		if (!$this->admin_db->insert($table, $log_arr)) {
			return false;
		}
		return true;
	}

	//获取账号信息
	public function getAdmin($admin_id){
		$table		= 'admin_user';
		$data		= array('admin_id'=>$admin_id);
        $query		= $this->admin_db->select()->from($table)->where($data)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }

	//获取操作信息
	public function getOperationLog($where){
		$table		= 'admin_action';
        $query		= $this->admin_db->select()->from($table)->where($where)->order_by('id','DESC')->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return array();
        }
    }
	
	//检查账号是否存在!
	public function checkAdmin($username,$password){
		$table		= 'admin_user';
		$password	= md5(md5($username.$password));
		$data		= array('admin_name'=>$username,'admin_password'=>$password,'admin_status'=>1);
        $query		= $this->admin_db->select()->from($table)->where($data)->limit(1)->get();
        if ($query->row()) {
            return $query->row_array();
        } else {
            return false;
        }
    }
}

?>